var express = require('express');
var router = express.Router();
const {
    Comment
} = require('../models')
const moment = require('moment')
    // 发表评论
router.post('/comments/:id', async function(req, res) {
    // 接收文章id
    const articleId = req.params.id

    // 接收评论内容
    const content = req.body.content

    // 登录的用户id(从jwt中获取)
    const userId = req.auth.user_id
    const username = req.auth.username

    // 发布时间
    const add_time = new Date()

    const result = await Comment.create({
        articleId,
        userId,
        add_time,
        content
    })

    // 添加一个选项
    result.dataValues.username = username
    result.dataValues.add_time = moment(add_time).format('YYYY-MM-DD HH:mm:ss')
    return res.cc(200, {
        code: 200,
        msg: '查询成功',
        data: result
    })
})


module.exports = router